package cloud.seri.iot.manager.web.rest

import cloud.seri.iot.common.model.CreateProductDTO
import cloud.seri.iot.common.model.UpdateProductDTO
import cloud.seri.iot.manager.domain.Product
import cloud.seri.iot.manager.service.ProductService
import cloud.seri.iot.manager.service.dto.ProductDTO
import cloud.seri.iot.manager.service.example.ProductExample
import com.aliyuncs.iot.model.v20180120.QueryProductResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import net.dreamlu.mica.core.result.R
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.net.URISyntaxException
import javax.validation.Valid

/**
 * REST controller for managing [cloud.seri.iot.manager.domain.Product].
 */
@RestController
@RequestMapping("/api")
@Api(tags = ["Product"], description = "产品相关接口")
class ProductResource(
    val productService: ProductService
)
{

    private val log = LoggerFactory.getLogger(this.javaClass)

    @Value("\${jhipster.clientApp.name}")
    private var applicationName: String? = null

    /**
     * `POST  /products` : Create a new product.
     *
     * @param productDTO the productDTO to create.
     * @return the [ResponseEntity] with status `201 (Created)` and with body the new productDTO, or with status `400 (Bad Request)` if the product has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/products")
    @ApiOperation("创建产品", nickname = "CreateProduct")
    fun createProduct(@Valid @RequestBody dto: CreateProductDTO): R<ProductDTO>?
    {
        return productService.save(dto)
    }

    /**
     * `PUT  /products` : Updates an existing product.
     *
     * @param productDTO the productDTO to update.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the updated productDTO,
     * or with status `400 (Bad Request)` if the productDTO is not valid,
     * or with status `500 (Internal Server Error)` if the productDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/products")
    @ApiOperation("更新产品", nickname = "UpdateProduct")
    fun updateProduct(@Valid @RequestBody productDTO: UpdateProductDTO): R<ProductDTO>?
    {
        return productService.update(productDTO)
    }

    /**
     * `GET  /products` : get all the products.
     *
     * @return the [ResponseEntity] with status `200 (OK)` and the list of products in body.
     */
    @GetMapping("/products")
    @ApiOperation("获取全部产品", nickname = "GetAllProducts")
    fun getAllProducts(page: Pageable, criteria: ProductExample?): Page<ProductDTO>
    {
        log.debug("REST request to get all Products")
        return productService.findAll(page, criteria)
    }


    @GetMapping("/products-list")
    @ApiOperation("获取全部产品List", nickname = "QueryProductList")
    fun queryProductList(): List<ProductDTO>
    {
        log.debug("获取全部产品")
        return productService.queryProductList()
    }

    /**
     * `GET  /products/:id` : get the "id" product.
     *
     * @param id the id of the productDTO to retrieve.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the productDTO, or with status `404 (Not Found)`.
     */
    @GetMapping("/products/{id}")
    @ApiOperation("查询产品详细信息", nickname = "QueryProduct")
    fun queryProduct(@PathVariable id: String): QueryProductResponse?
    {
        log.debug("REST request to get Product : {}", id)
        return productService.findOne(id)
    }

    /**
     * `DELETE  /products/:id` : delete the "id" product.
     *
     * @param id the id of the productDTO to delete.
     * @return the [ResponseEntity] with status `204 (NO_CONTENT)`.
     */
    @DeleteMapping("/products/{id}")
    @ApiOperation("删除产品", nickname = "DeleteProduct")
    fun deleteProduct(@PathVariable id: String): R<Void>
    {
        log.debug("REST request to delete Product : {}", id)
        return productService.delete(id)
    }


    companion object
    {
        private const val ENTITY_NAME = "seriIotManagerProduct"
    }
}
